package ltd.ninefish.framework.core.web.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import ltd.ninefish.framework.core.result.Result;
import ltd.ninefish.framework.core.utils.PageUtil;
import ltd.ninefish.framework.core.web.page.PageData;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.List;

/**
 * @author zhaoxiubin
 * create-time 2023-12-16 20:26
 * description Controller基类
 **/

public class BaseController {

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        // LocalDateTime 类型转换
        binder.registerCustomEditor(LocalDateTime.class, new PropertyEditorSupport()
        {
            @Override
            public void setAsText(String dateTime)
            {
                LocalDateTime ldt = null;
                try{
                    ldt = DateUtil.parseLocalDateTime(dateTime, "yyyy-MM-dd HH:mm:ss");
                }catch (DateTimeParseException e){
                    try{
                        ldt = DateUtil.parseLocalDateTime(dateTime, "yyyy-MM-dd HH:mm");
                    }catch (DateTimeParseException ex){
                        try{
                            ldt = DateUtil.parseLocalDateTime(dateTime, "yyyy-MM-dd'T'HH:mm:ssXXX");
                        }catch (DateTimeParseException exc){
                            try{
                                ldt = DateUtil.parseLocalDateTime(dateTime, "yyyy-MM-dd HH:mm:ss:SSS");
                            }catch (DateTimeParseException excp){
                                try{
                                    ldt = DateUtil.parseLocalDateTime(dateTime, "yyyy-MM-dd'T'HH:mm:ss.SSS");
                                }catch (DateTimeParseException excpt){

                                }
                            }
                        }
                    }
                }
                setValue(ldt);
            }
        });
        // LocalDate 类型转换
        binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport()
        {
            @Override
            public void setAsText(String date)
            {
                LocalDate ld = null;
                try{
                    ld = LocalDate.from(DateUtil.parseLocalDateTime(date, "yyyy-MM-dd"));
                }catch (DateTimeParseException e){
                    try{
                        ld = LocalDate.from(DateUtil.parseLocalDateTime(date, "yyyy/MM/dd"));
                    }catch (DateTimeParseException ex){
                        try{
                            ld = LocalDate.from(DateUtil.parseLocalDateTime(date, "yyyyMMdd"));
                        }catch (DateTimeParseException exc){
                            try{
                                ld = LocalDate.from(DateUtil.parseLocalDateTime(date, "yyyy-MM"));
                            }catch (DateTimeParseException excp){

                            }
                        }
                    }
                }
                setValue(ld);
            }
        });
        // LocalDateTime 类型转换
        binder.registerCustomEditor(LocalTime.class, new PropertyEditorSupport()
        {
            @Override
            public void setAsText(String time)
            {
                LocalTime lt = null;
                try{
                    lt = LocalTime.from(DateUtil.parseLocalDateTime(time, "HH:mm:ss"));
                }catch (DateTimeParseException e){
                    try{
                        lt = LocalTime.from(DateUtil.parseLocalDateTime(time, "HH:mm"));
                    }catch (DateTimeParseException ex){
                        try{
                            lt = LocalTime.from(DateUtil.parseLocalDateTime(time, "HH:mm:ss:SSS"));
                        }catch (DateTimeParseException exc){

                        }
                    }
                }
                setValue(lt);
            }
        });
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageUtil.startPage();
    }

    /**
     * 清理分页的线程变量
     */
    protected void clearPage()
    {
        PageUtil.clearPage();
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected PageData getPageData(List<?> list)
    {
        PageData pageData = new PageData();
        pageData.setList(list);
        PageInfo pageInfo = new PageInfo(list);
        pageData.setTotal(pageInfo.getTotal());
        pageData.setPageNum(pageInfo.getPageNum());
        pageData.setPageSize(pageInfo.getPageSize());
        pageData.setTotalPage(pageInfo.getPages());
        return pageData;
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected Result result(int rows)
    {
        return rows > 0 ? Result.ok() : Result.fail();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected Result result(boolean result)
    {
        return result ? ok() : fail();
    }

    /**
     * 返回成功
     */
    public Result ok()
    {
        return Result.ok();
    }

    /**
     * 返回失败消息
     */
    public Result fail()
    {
        return Result.fail();
    }

    /**
     * 返回成功
     */
    public Result ok(String message)
    {
        return Result.ok(message);
    }

    /**
     * 返回失败消息
     */
    public Result fail(String message)
    {
        return Result.fail(message);
    }
}
